import copy
import pandas as pd
from STool.StockTool import StockTool
from STool.StockTT import *



class StockMT:
    def __init__(self,sql_obj):
        self.sql_obj = sql_obj
        print("")
    

    def RPS(self,stocks):#股票RPS
        pd_meta_stocks = copy.deepcopy(stocks)
        pd_rps_stocks = copy.deepcopy(stocks)
        for rps_k in [3,5,20]: #一次性计算RPS5 RPS20 RPS60 RPS250的数据
            rps_type="avail_"+str(rps_k)
            rps_key="rps"+str(rps_k)
            #根据 N日涨跌幅降序
            a = pd_meta_stocks.sort_values(by=rps_type,ascending=False)
            a['n']=list(range(1,len(a)+1))
            rps=(1-a['n']/len(a))*100.0
            rps = round(rps, 2)  # 保留2位小数
            pd_rps_stocks[rps_key]=rps  
        
        print("计算所有RPS数据完成")
        print("\n")
        return pd_rps_stocks

    def BKPower(self,pd_stock_rps):
        pd_stocks = copy.deepcopy(pd_stock_rps)
        #按照板块数据重排
        pd_stocks=pd_stocks.sort_values(by="hy")
        bk_power={}
        all_bk= self.sql_obj.stock_all_bankuai_sw1_stock_count #获取所有板块数据
        for bk in list(all_bk.keys()):
            value_stocks_len = len(pd_stocks[pd_stocks['hy'] == bk])# 当前板块股票
            all_stocks_len = all_bk[bk]
            if value_stocks_len > 0:# 当前板块股票个数
                stock_mt_ratio=float(value_stocks_len)/float(all_stocks_len)*100.01/100 #上榜占比
                mt=round(value_stocks_len*stock_mt_ratio,2)#板块动量分值
                if stock_mt_ratio>=0.05:
                    bk_power[bk]=stock_mt_ratio
        
        #获取板块动量大于1的板块数据
        bk_power_dic = StockTool.listToDic(bk_power)
        return bk_power_dic
    
    def PPivot(self,stocks):
        pd_stocks = copy.deepcopy(stocks)
        # print(pd_stocks)
        # print(list(pd_stocks.is20Wave.values))

        # # pd_stocks=pd_stocks[(pd_stocks['rps20']>=90)|( pd_stocks['rps60']>=90)]#rps20 或者90 任一一个大于90
        pd_stocks=pd_stocks[pd_stocks['rps5'] >= 85]#rps5 大于90
        # pd_stocks=pd_stocks[pd_stocks['is20High'] >= 1]#收盘价20日新高
        pd_stocks=pd_stocks[pd_stocks['is20Wave'] >= 1]#收盘价20日振幅小于20%
        pd_stocks=pd_stocks[pd_stocks['percent'] >= 9]#当日涨幅大于6%
        pd_stocks=pd_stocks[pd_stocks['vol']>=pd_stocks['maxvol'] *1.1] #当日交易量是前10日平均交易量的1.1倍        
       

        return pd_stocks
        
        
    
       
